package web.controller.address_telefon;

import java.util.ArrayList;
import java.util.List;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Telephone;
import hibernate.pojo_address_telefon.TelephoneBook;
import hibernate.pojo_address_telefon.TelephoneTypes;
import hibernate.util.HibernateUtil;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

public class CreateNewTelephoneRecordController {

	private static final Log logger = LogFactory.getLog(CreateNewTelephoneBookController.class);
	
	private String areaCode;
	private String phoneNumber;
	private String name;
	private TelephoneBook telephoneBook;
	private TelephoneTypes telephoneTypes;
	private Long listelemeSelectedTelephoneTypeId = new Long(0);
	private List<SelectItem> telephoneTypesItemsList;
	private List<TelephoneTypes> telephoneTypesList;
	
	public CreateNewTelephoneRecordController() {
		// TODO Auto-generated constructor stub
	}
	
	public String insertNewTelephoneRecord(){
		
		String pageToRedirect = "";
		int userID;
		int telephoneBookID = 0;
		
		if(!validateTelephoneNumber()){
			logger.info("Telefon numarasi gecerli degil!");
			pageToRedirect = ""; //show warning message
			return pageToRedirect;
		}
		
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			userID = ((User)context.getExternalContext().getSessionMap().get("user")).getId();
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(TelephoneBook.class);
			criteria.add(Restrictions.eq("userID",userID));

			List<TelephoneBook> telephoneBookFromDB = criteria.list();
			if(telephoneBookFromDB == null || telephoneBookFromDB.size() == 0){
				logger.info("Kullanicinin kayitli telefon defteri bulunmamaktadir!");
				pageToRedirect = "";  //show warning message
				return pageToRedirect;
			}
			if(telephoneBookFromDB != null || telephoneBookFromDB.size() == 1){ // kullanicinin kayitli telefon defteri varsa, telefon ekle
				
				telephoneBook = (TelephoneBook)criteria.list().get(0);
				telephoneBookID = telephoneBook.getId();
				
				Telephone telephone = new Telephone();
				telephone.setAreaCode(areaCode);
				telephone.setPhoneNumber(phoneNumber);
				telephone.setName(name);
				telephone.setTelephoneBookID(telephoneBookID);
				telephone.setTelephoneBook(telephoneBook);
				if(getListelemeSelectedTelephoneTypeId() != null){
					int selectedTelephoneTypeIdAtDb = (int) (getListelemeSelectedTelephoneTypeId()-1);
					telephone.setTelephoneTypeID((selectedTelephoneTypeIdAtDb+1));
					telephone.setTelephoneType(telephoneTypesList.get(selectedTelephoneTypeIdAtDb));
				
				}
				
				Transaction transaction = session.beginTransaction();
				session.save(telephone);
				transaction.commit();
				clearFields();
				pageToRedirect = "createdNewTelephoneEntrySuccess";
				
			}
			
			
		} catch (Exception exception) {
			logger.error("Yeni telefon kaydi yaparken hata olustu!", exception);
		}
		
		
		return pageToRedirect;
	}
	
	public boolean validateTelephoneNumber(){
		
		
		return true;
	}
	
	public void clearFields(){
		this.areaCode = "";
		this.phoneNumber = "";
		this.name = "";
	}
	
	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setTelephoneTypes(TelephoneTypes telephoneTypes) {
		this.telephoneTypes = telephoneTypes;
	}

	public TelephoneTypes getTelephoneTypes() {
		return telephoneTypes;
	}

	public void setListelemeSelectedTelephoneTypeId(
			Long listelemeSelectedTelephoneTypeId) {
		this.listelemeSelectedTelephoneTypeId = listelemeSelectedTelephoneTypeId;
	}

	public Long getListelemeSelectedTelephoneTypeId() {
		return listelemeSelectedTelephoneTypeId;
	}

	public void setTelephoneTypesItemsList(List<SelectItem> telephoneTypesItemsList) {
		this.telephoneTypesItemsList = telephoneTypesItemsList;
	}

	public List<SelectItem> getTelephoneTypesItemsList() {
		
		if (telephoneTypesItemsList == null) {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(TelephoneTypes.class);
			telephoneTypesList = criteria.list();
			telephoneTypesItemsList = telephoneTypesListToSelectItemList(telephoneTypesList);
		}
		return telephoneTypesItemsList;
	}
	
	public static List<SelectItem> telephoneTypesListToSelectItemList(List<TelephoneTypes> telephoneTypesList) {

		List<SelectItem> returnTelephoneTypesSelectItemList = new ArrayList<SelectItem>();
		returnTelephoneTypesSelectItemList.add(new SelectItem(new Long(0), "Select"));
		if (telephoneTypesList != null && telephoneTypesList.size() > 0) {
			for (TelephoneTypes telephoneTypes : telephoneTypesList) {
				SelectItem selectItem = new SelectItem(telephoneTypes.getId(), telephoneTypes.getDescription());
				returnTelephoneTypesSelectItemList.add(selectItem);
			}
		}
		return returnTelephoneTypesSelectItemList;
	}

}
